package com.ruoyi.web.controller.api;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.SensorDataService;
import com.ruoyi.system.utils.ClassMethodNameUtil;
import com.ruoyi.system.utils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 传感器数据处理
 *
 * @author lsw
 */
@RestController
@RequestMapping("/system/sensor")
public class SensorController {

    @Autowired
    private SensorDataService sensorDataService;

    @Autowired
    private LogUtil logUtil;

    /**
     * 数据上传
     * {"dataStatus":0,"devPort":11001,"gateWayIP":"192.168.1.88","metric_time":"2023-09-18 23:41:59","regisData":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,560,0,0,0,0],"startAdd":"0"}
     * <p>
     * ("dataStatus" :-1,"devPort":“11001","gateWayIP" :"192.168.0.91","regisData":[0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0 0,0,] "startAdd":"0"}
     */
    @PostMapping("/dataUpload")
    public AjaxResult dataUpload(@RequestBody JSONObject jsonObject) {
        String msg = "【dataUpload】网关设备上行数据：" + jsonObject;
        logUtil.info(ClassMethodNameUtil.getSimpleMethodName(), msg);
        sensorDataService.processGatewayUpstreamData(jsonObject);
        return AjaxResult.success(true);
    }

    /**
     * 心跳检测
     * {"device_ip":"192.168.1.88","timestamp":"707711","interfaces":[{"name":"eth0","status":"up"},{"name":"eth1","status":"down"}]}
     */
    @PostMapping("/heartbeat/detection")
    public AjaxResult heartbeatDetection(@RequestBody JSONObject jsonObject) {
        String msg = "【网关心跳检测】接收时间：" + DateUtils.getTime() + "，接收数据：" + jsonObject;
        logUtil.info(ClassMethodNameUtil.getSimpleMethodName(), msg);
        sensorDataService.heartbeatDetection(jsonObject);
        return AjaxResult.success(true);
    }

    /**
     * 网关下发配置
     * {"msg":"操作成功","code":200,"data":{"up_ports":[{"baudrate":9600,"data":[{"reg_num":33,"slave_addr":1,"func_code":2,"start_addr":65},{"reg_num":38,"slave_addr":2,"func_code":3,"start_addr":55}],"serial_port":0,"dev_port":11001},{"baudrate":9600,"data":[{"reg_num":33,"slave_addr":1,"func_code":2,"start_addr":65}],"serial_port":2,"dev_port":11003},{"baudrate":9600,"data":[{"reg_num":33,"slave_addr":1,"func_code":2,"start_addr":65}],"serial_port":3,"dev_port":11004},{"baudrate":9600,"data":[{"reg_num":33,"slave_addr":1,"func_code":2,"start_addr":65}],"serial_port":4,"dev_port":11005},{"baudrate":9600,"data":[{"reg_num":33,"slave_addr":1,"func_code":2,"start_addr":65}],"serial_port":5,"dev_port":11006}],"down_ports":[{"baudrate":38400,"listen_port":502,"serial_port":1,"dev_port":11002}]}}
     */
    @GetMapping("/configuration")
    public AjaxResult configuration(String dev_sn) {
        String msg = "【网关下发配置】接收时间：" + DateUtils.getTime() + "，接收数据：" + dev_sn;
        logUtil.info(ClassMethodNameUtil.getSimpleMethodName(), msg);
        JSONObject jsonObject = sensorDataService.configuration(dev_sn);
        msg = "【网关下发配置】发送时间：" + DateUtils.getTime() + "，发送数据：" + jsonObject;
        logUtil.info(ClassMethodNameUtil.getSimpleMethodName(), msg);
        return AjaxResult.success(jsonObject);
    }
}
